package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ExtCproductDao extCproductDao;
    /*
     购销合同分页
     */
    @Override
    public PageInfo<Contract> findByPage(ContractExample contractExample, int pageNum, int pageSize) {
        //设置开始页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //查询全部
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        PageInfo<Contract> pageInfo = new PageInfo<>(contractList);
        return pageInfo;
    }

    //查询全部
    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        return contractList;
    }

    //根据主键查询
    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    // 保存购销合同
    @Override
    public void save(Contract contract) {
        contract.setId(UUID.randomUUID().toString());
        //购销合同创建时间
        contract.setCreateTime(new Date());
        //购销合同的更新时间
        contract.setUpdateTime(new Date());
        contractDao.insertSelective(contract);
    }

    //更新购销合同
    @Override
    public void update(Contract contract) {
        //购销合同的更新时间
        contract.setUpdateTime(new Date());
        contractDao.updateByPrimaryKeySelective(contract);
    }

    //删除购销合同（需求8）
    //0(草稿状态，草稿状态合同不算是正式合同)  1(已上报，这种合同已经成为一个真正的合同，允许生成报运单)   2 （该合同已经生成了报运单）
    @Override
    public void delete(String id) {
        Contract contract = contractDao.selectByPrimaryKey(id);
        if (contract.getState()!=null&&contract.getState()!=0) {
            //1、判断该购销合同的状态是否为0，如果不为0则不能被删除，暂时直接return
            return;
        }

        //2、如果为0,或者为空
        //2.1删除该购销合同下的所有商品
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdEqualTo(id);
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
        if (contractProductList!=null) {
            for (ContractProduct contractProduct : contractProductList) {
                contractProductDao.deleteByPrimaryKey(contractProduct.getId());
            }
        }
        //2.2删除该购销合同下的所有附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdEqualTo(id);
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
        if (extCproductList!=null) {
            for (ExtCproduct extCproduct : extCproductList) {
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }
        //2.3删除该购销合同
        contractDao.deleteByPrimaryKey(id);
    }

    //查询本部门以及下属部门的购销合同
    @Override
    public PageInfo<Contract> findPageByDeptId(String companyId, String deptId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> contractList = contractDao.findPageByDeptId(companyId,deptId);
        return new PageInfo<>(contractList);
    }

    @Override
    public List<String> findAllDeliveryPeriod() {
        return contractDao.findAllDeliveryPeriod();
    }

    /**
     * 传入的是当天的时间
     * @param date
     * @return
     */
    @Override
    public List<Contract> findContractAfterThreeDays(Date date) {
        //创建日历对象并把当前日期往后推迟3天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,3);
        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(time);
        List<Contract>contractList=contractDao.findContractAfterThreeDays(formatDate);
        if (contractList!=null&&contractList.size()>0){
            return contractList;
        }
        return null;
    }
}
